
#include "stdafx.h"
#include "CFileInfo.h"
#include "KWow64FsRedrt.h"
#include "md5.h"
#include <boost/format.hpp>
#include <string>
#include "../publicfunciton.h"

#pragma warning (disable:4996) 

PFN_TRUSTVERIFY							g_pfnTrustVerify;

CFileInfo::CFileInfo()
{
	g_pfnTrustVerify = NULL;
	m_handleTrust	 = NULL;
};

CFileInfo::~CFileInfo()
{
	if ( m_handleTrust )
	{
		::FreeLibrary(m_handleTrust);
	}
};

BOOL CFileInfo::InitDLL(LPCTSTR VRVTrustVerifydllPath)
{
	m_handleTrust = ::LoadLibrary(VRVTrustVerifydllPath);
	if (!m_handleTrust)
	{
		return FALSE;
	}
	g_pfnTrustVerify = (PFN_TRUSTVERIFY)::GetProcAddress(m_handleTrust,"TrustVerify");
	if (!g_pfnTrustVerify)
	{
		return FALSE;
	}
	return TRUE;
}

String CFileInfo::GetFingerPrint( TCHAR *Path)
{
	BYTE byMd5[16] = {0};
	char md5[34] = {0};
	int iRet;

	CKWow64FsRedrt g_Kfr;
	g_Kfr.Init();
	g_Kfr.Close();
	iRet = ::GetFingerPrint(Path,byMd5,GetTickCount());
	g_Kfr.Open();

	BytesToChar(byMd5,md5,16);

	if (iRet == -1)
	{
		return _T("");
	}
#ifdef UNICODE
		return EasyStr<ascii>(md5).To<unicode16>();
#else
		return String(md5);
#endif
	
}

long CFileInfo::GetFileInfo ( TCHAR *Path, PFILE_INFO pFileInfo )
{
	long lRet = -1;

	CKWow64FsRedrt			g_Kfr;
	g_Kfr.Init();
	g_Kfr.Close();
	lRet = ::GetFileInfo(Path,GetTickCount(),pFileInfo);
	g_Kfr.Open();

	return lRet;
}
int  CFileInfo::TrustVerify(char *szFileName,char *szSubjectName,char *szIssuerName)
{
	long lRet = -1;
	if (g_pfnTrustVerify)
	{
		CKWow64FsRedrt g_Kfr;
		g_Kfr.Init();
		g_Kfr.Close();
		lRet =  g_pfnTrustVerify(szFileName,szSubjectName,szIssuerName,GetTickCount());
		g_Kfr.Open();

	}
	return lRet;
}

bool CFileInfo::BytesToChar( BYTE byMd5[], char* chMd5,int len)
{
	for(int i = 0; i < len; i++)
	{
		sprintf_s(&chMd5[i*2],4,"%02x",byMd5[i]);
	}
	return true;
}
